<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ajax_model extends CI_Model
{
  function __construct() {
	  parent::__construct();
  }
  function get_last_order(){
	  $this->db->order_by('CartID','DESC');
	  $this->db->where('Status !=','Status');
	  $this->db->where('IsDelete',0);
	  return $this->db->get('shoppingcart',5,0)->result();
  }
  /****
  * Thong bao chuyen khoan moi nhat
  */
  
  function get_transfer_notice_last(){
	  $this->db->select('account.*,transfernotice.*');
	  $this->db->join('account','account.AccountID = transfernotice.AccountID');
	  $this->db->order_by('transfernotice.TransferID','DESC');
	  $this->db->limit('10');
	  return $this->db->get('transfernotice')->result();
  }
  
  /*****
  * Tinh tong san pham trong moi don hang
  * 
  * @param mixed $CartID
  */
  function get_sum_product($CartID){
	  $this->db->select_sum('Quantity','total');
	  $this->db->from('cartdetail');
	  $this->db->where('CartID',$CartID);
	  $query = $this->db->get();
	  return $query->row()->total;
  }
  /********
  * Tinh tong tien trong moi don hang
  * 
  * @param mixed $CartID
  */
  function get_sum_price($CartID){
	   $this->db->where('CartID',$CartID);
	   $list = $this->db->get('cartdetail')->result();
	   $total = 0;
	   foreach($list as $rs):
			$total = $total + ($rs->Price * $rs->Quantity);
	   endforeach;
	  return $total;
  }  
  
/********
* Upload img           
*/
  function add_img($filename,$ProductID,$session){
	  if($ProductID!=0){
		  if($filename!=''){
			  fn_resize_image('../uploads/images/'.$filename,'../uploads/product/200/'.$filename,200,200);
			  fn_resize_image('../uploads/images/'.$filename,'../uploads/product/80/'.$filename,80,80);
			  fn_resize_image('../uploads/images/'.$filename,'../uploads/product/40/'.$filename,40,40);
			  fn_resize_image('../uploads/images/'.$filename,'../uploads/product/30/'.$filename,30,30);
		  }  
	  }  
	  $data = array(
		'ImagePath' => $filename,
		'ProductID' => (int)$ProductID,
		'ImageSession' => ($ProductID!=0)?'':$session
	  );
	  $this->db->insert('productimage',$data);
	  
	  return $this->db->insert_id();;
  }
  /*
   * @author : Vo Van Hieu
   * @method : set active/deactive
   * @param  :
   * @date   : 2/10/2012
   * @modify : 
   */
  function setActiveDeactive($table,$field,$id,$pub){
	  $this->db->set('bl_active',$pub);
	  $this->db->where($field,$id);
	  $this->db->update($table);
	}
}
?>
